<?php
//var_dump();
class AdMoneyExchangeLog  extends BaseModel
{
	public $res;
	public $redis_key="";
	public function getSource()
		    {
				return "ad_money_exchange_log";
    }
	public function initialize()
	{
		parent::initialize();

	}
	public function get_last_day_pond($user_id){
		$day=1;
		$date=date('Y-m-d',strtotime("-".$day." day"));
		$condition=['conditions'=>"user_id=$user_id and  changge_type='yuanbao_to_cash' and   date >='{$date} 00:00:00' and date <= '{$date} 23:59:59'",'columns'=>'new_coin'];
//	var_dump($condition);	die;
		$res=$this->findFirst($condition);
		if($res){
		return $res->new_coin;
		}
		return 0;

	}
	public function get_money($user_id,$time){
		$data=[];
		foreach($time as $k =>$v){
			$condition=['conditions'=>"user_id=$user_id and  changge_type like '%cash' and   date BETWEEN '{$v['start_at']}'  and '{$v['end_at']}'",'columns'=>'sum(new_coin) as new_coin'];

			$res=$this->findFirst($condition);
			if($res != false){
			$data[$k]=doubleval($res->new_coin);
			}else{
			$data[$k]=0;
			}
		}
return $data;
	}


    /**
     * 获取广告收益
     * @param $user_id
     * @param $time
     * @return array
     */
    public function get_money_coin($user_id,$time){
        $condition = "user_id={$user_id} and changge_type  like '%cash' ";
        if($time){
            $condition .= " AND date BETWEEN '{$time['start_at']}' and '{$time['end_at']}'";
        }
        $res=$this::SUM(array(
            $condition,
            'column'=>'new_coin'
        ));
        if($res){
            return doubleval($res);
        }else{
            return 0.00;
        }
    }


    /**
     * 蓝宝石兑换记录
     */
    public function yuanbaolist($params=[]){
        $condition = 'user_id<>1005';
        if(isset($params['changge_type']) && !empty($params['changge_type'])){
            if($condition){
                $condition .= " AND changge_type in({$params['changge_type']})";
            }else{
                $condition = "changge_type in  in({$params['changge_type']})";
            }
        }
        if(isset($params['user_id']) && !empty($params['user_id'])){
            if($condition){
                $condition .= " AND user_id='{$params['user_id']}'";
            }else{
                $condition="user_id='{$params['user_id']}'";
            }
        }

        if(isset($params['start_time']) && isset($params['end_time'])){
            if($condition){
                $condition .= " AND date >= '{$params['start_time']}' AND date <= '{$params['end_time']}'";
            }else{
                $condition = "date >= '{$params['start_time']}' AND date <= '{$params['end_time']}'";
            }
        }

        if(isset($params['order']) && !empty($params['order'])) {
            $order = $params['order'];
        }else{
            $order = 'id desc';
        }

        $list = $this->find(
            array(
                $condition,
                'order' => $order,
            )
        );
        return $list;
	}
	/*
	 用户广告核账
	 */
	public function get_user_money($date){
		$condition['conditions']="date >= '{$date['start_at']}' and date <='{$date['end_at']}' and changge_type in ('gold_to_cash','yuanbao_to_cash','ad_to_cash') ";
		$condition['columns']='user_id,sum(new_coin) as money';
		$condition['group']='user_id';
		$res=$this->find($condition)->toarray();

		if($res == []){
		die('没有广告收益');
	}
		return $res;
	}

    //批量添加
    public function batch_insert(array $data)
    {
        if (count($data) == 0) {
            throw new \Exception('参数错误');
        }
        $keys = array_keys(reset($data));
        $keys = array_map(function ($key) {
            return "`{$key}`";
        }, $keys);
        $keys = implode(',', $keys);
        $sql  = "INSERT INTO " . $this->getSource() . " ({$keys}) VALUES ";
        foreach ($data as $v) {
            $v      = array_map(function ($value) {
                return "'{$value}'";
            }, $v);
            $values = implode(',', array_values($v));
            $sql    .= " ({$values}), ";
        }
        $sql = rtrim(trim($sql), ',');
        //DI中注册的数据库服务名称为"db"
        $result = $this->getDI()->get('db')->execute($sql);

        if (!$result) {
            return false;
        }
        return $result;
    }
}
